package com.team24.hotel.controller;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.team24.hotel.domain.*;
import com.team24.hotel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/frontOrders")
@Controller
public class FrontOrdersController{
    @Autowired
    private FrontOrderService frontOrderService;

    @RequestMapping("/selectAll")
    public String findOrderByUserId(Model model , HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        List<Order> orders = frontOrderService.selectAllByUserId(user.getUserId());
        model.addAttribute("order",orders);
        return "frontOrders";
    }
    @RequestMapping("/selectByOrderId")
    public  String listOrderDetailPage(Model model ,String id){
        System.out.println(id);
        Order order=frontOrderService.selectExtByPrimaryKey(id);
        model.addAttribute("order",order);
        return "frontOrderDetail";
    }
    @RequestMapping("/history")
    public String historyList( Model model, HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        List<Order> order = frontOrderService.selectHistoryAll(user.getUserId());
        model.addAttribute("order" ,order);
        return "frontOrdersHistory";
    }

    @RequestMapping("/toCancel")
    public String toCancel(String id) {
        frontOrderService.updateOrderStatusToCancel(id);
        return "redirect:/frontOrders/history";
    }

}
